package com.zztl.infiniteBand.receiver

import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.zztl.infiniteBand.eventbus.BluetoothSwitchEvent
import com.zztl.infiniteBand.util.LogUtils
import org.greenrobot.eventbus.EventBus

/**
 *  @desc
 *  @auth ${user}
 *  @time 2018/10/25  11:31
 */
class BluetoothConnectReciver : BroadcastReceiver() {
    private var isFirstSync = true

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action

        LogUtils.i("swl", "intent.getAction() == " + action!!)
        if (action == "android.bluetooth.device.action.ACL_CONNECTED") {
//            EventBus.getDefault().post(ConnectedEvent(true))

        } else if (action == "android.bluetooth.device.action.ACL_DISCONNECTED") {

            LogUtils.i("bluetooth", "==== 收到断开连接的广播通知 =====")
//            EventBus.getDefault().post(ConnectedEvent(false))

        } else if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
            val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                    BluetoothAdapter.ERROR)
            when (state) {
                BluetoothAdapter.STATE_OFF -> {
//                    EventBus.getDefault().post(ConnectedEvent(false))
//                    EventBus.getDefault().post(SyncStepEvent(BraceletApplication.watchCountStep))
                    EventBus.getDefault().post(BluetoothSwitchEvent(true))
                }
                BluetoothAdapter.STATE_ON -> {
                    EventBus.getDefault().post(BluetoothSwitchEvent(true))
                    LogUtils.i("swl", "STATE_OFF 手机蓝牙打开")
                }
            }
        }
    }



}